package com.xindaoapp.happypet.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.GoodsInfo;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.ProgressHUD;

/**
 * 商品评价
 * 
 * @author Administrator
 * 
 */
public class OrderEvaluateActivity extends UMBaseActivity implements OnClickListener {
    private final String TAG = "OrderEvaluateActivity";
    private Context mContext;
    private ImageButton back;
    private TextView fabiao;

    private TextView time;

    private ImageView icon;
    private TextView title;
    private TextView category;// 分类
    private TextView price;
    private TextView num;

    private ImageView arrow;

    private RatingBar bar1;

    private EditText shuru;

    private View line;

    private GoodsInfo info;

    // int num1;

    private String time1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_evaluate);
        mContext = OrderEvaluateActivity.this;
        info = (GoodsInfo) getIntent().getSerializableExtra("object");
        time1 = getIntent().getStringExtra("time");

        initView();
        initData();
    }

    private void initView() {
        back = (ImageButton) findViewById(R.id.cancel);
        fabiao = (TextView) findViewById(R.id.fabiao);

        time = (TextView) findViewById(R.id.evaluate_time);

        icon = (ImageView) findViewById(R.id.order_icon);
        title = (TextView) findViewById(R.id.order_title);
        category = (TextView) findViewById(R.id.order_category);
        price = (TextView) findViewById(R.id.order_price);
        num = (TextView) findViewById(R.id.order_num);
        arrow = (ImageView) findViewById(R.id.iv);

        line = findViewById(R.id.line);

        bar1 = (RatingBar) findViewById(R.id.evaluate_1);
        shuru = (EditText) findViewById(R.id.shuru);
    }

    private void initData() {
        line.setVisibility(View.GONE);
        category.setVisibility(View.GONE);
        arrow.setVisibility(View.GONE);
        num.setVisibility(View.GONE);
        back.setOnClickListener(this);
        fabiao.setOnClickListener(this);

        // bar1.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        // @Override
        // public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        // num1 = (int) rating;
        // }
        // });

        time.setText("成交时间:" + time1);
        ImageLoader.getInstance().displayImage(info.goods_thumb, icon);
        title.setText(info.goods_name);
        price.setText("￥" + String.valueOf(info.goods_price));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.fabiao:
                String msg = shuru.getText().toString();
                if (bar1.getRating() == 0) {
                    Toast.makeText(mContext, "请给商品评下分,谢谢", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(msg)) {
                    Toast.makeText(mContext, "评价内容不能为空,谢谢", Toast.LENGTH_SHORT).show();
                    return;
                }
                sendMsg(msg);
                break;
            default:
                break;
        }
    }

    private void sendMsg(String msg) {
        if (CommonUtil.checkNetState(mContext)) {
            RequestParams params = new RequestParams();
            params.put("userID", CommonUtil.decode(UserState.getUserUid()));
            params.put("commentRank", CommonUtil.decode(bar1.getRating() + ""));
            params.put("content", CommonUtil.decode(msg));
            params.put("goodsID", CommonUtil.decode(info.goods_id));
            params.put("orderID", CommonUtil.decode(info.order_id));
            params.put("user_name", CommonUtil.decode(UserState.getUser().username));
            params.add("version", "1.0");

            final ProgressHUD progressHUD = ProgressHUD.show(mContext, "正在加载...", true, true, null);
            AddClient.get(Address.COMMENT, params, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, String content) {
                    if (null != progressHUD && progressHUD.isShowing()) {
                        progressHUD.dismiss();
                    }
                    int status = -1;
                    String msg = null;
                    JSONObject obj;
                    try {
                        obj = new JSONObject(content);
                        status = obj.getInt("status");
                        msg = obj.getString("msg");
                        if (status == 1) {
                            Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(getBaseContext(), MyOrderListActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                        } else {
                            Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                        }
                        finish();
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Throwable error, String content) {
                    if (null != progressHUD && progressHUD.isShowing()) {
                        progressHUD.dismiss();
                    }
                    super.onFailure(error, content);
                }
            });
        } else {
            Toast.makeText(mContext, R.string.pLease_check_network, 0).show();
            return;
        }
    }
}
